//
//  ViewController.m
//  手势的应用
//
//  Created by 伍银荣 on 2021/7/9.
//  Copyright © 2021 com.WYR. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

/**
 常见的手势:
 1. UITapGestureRecognizer  (敲击)
 2. UISwipeGestureRecognizer  (轻扫)
 3. UILongPressGestureRecognizer  (长按)
 4. UIPanGestureRecognizer  (拖拽)
 5. UIPinchGestureRecognizer   (捏合, 缩放)
 6. UIRotationGestureRecognizer  (旋转)
 */

//手指开始触摸view的时候
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self tap];
 //   [self longPress];
  //  [self pan];
  //  [self swipe];
   // [self rotate];
    [self pinch];
}

#pragma mark - 捏合(缩放)
- (void)pinch {
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];
    [self.imageView addGestureRecognizer:pinch];
}

//捏合的实现
- (void)pinch:(UIPinchGestureRecognizer *)sender {
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, sender.scale, sender.scale);
    //归零,恢复到初始状态
    sender.scale = 1;
    
}

#pragma mark - 旋转(必须要两根手指旋转)
- (void)rotate {
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [self.imageView addGestureRecognizer:rotation];
}

//旋转的实现方法
- (void)rotation:(UIRotationGestureRecognizer *)sender {
    NSLog(@"rotation = %f",sender.rotation);
    //这个方法是在之前转到的角度上增加角度
    self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, sender.rotation);
    //归零,恢复到初始状态(将之前转到的角度设置为0,这样就不会导致旋转的角度和实际旋转不一样)
    sender.rotation = 0;
}

#pragma mark - 轻扫
- (void)swipe {
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    //swipe.numberOfTouchesRequired = 2; //需要的手指数量
    /**
    UISwipeGestureRecognizerDirectionRight = 1 << 0,  从右向左轻扫
    UISwipeGestureRecognizerDirectionLeft  = 1 << 1,   从左往右轻扫  (默认)
    UISwipeGestureRecognizerDirectionUp    = 1 << 2,  从下往上
    UISwipeGestureRecognizerDirectionDown  = 1 << 3   从上往下
     */
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;
    
    [self.imageView addGestureRecognizer:swipe];
}

//实现方法
- (void)swipe:(UISwipeGestureRecognizer *)sender {
    NSLog(@"轻扫");
}


#pragma mark - 拖拽
- (void)pan {
   //创建手势识别对象
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    /**
     UIPanGestureRecognizer的属性:
     1. minimumNumberOfTouches:最少需要几根手指才能响应,默认为1
     2. pan.maximumNumberOfTouches:最多需要几根手指才能响应,可以取到最大数
     */
  //  pan.minimumNumberOfTouches = 2;
//    pan.maximumNumberOfTouches = 3;
   
    [self.imageView addGestureRecognizer:pan];
}

//脱拽的响应(实现方法)
- (void)pan:(UIPanGestureRecognizer *)sender {
    NSLog(@"拖拽了");
    //拖拽到哪个位置
        CGPoint p = [sender translationInView:sender.view];
        NSLog(@"x = %f, y = %f",p.x, p.y);
    //不带make的是在之前的基础上累加
    self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, p.x, p.y);
    //恢复到初始状态
    [sender setTranslation:CGPointZero inView:sender.view];
}


#pragma mark - 长按
- (void)longPress {
    //创建手势识别对象
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    
    /**
     UILongPressGestureRecognizer的属性:
     1. numberOfTapsRequired: 需要完全按下去几次才会响应.默认为0次
     2. numberOfTouchesRequired: 需要几根手指按下去才会响应, 默认为1根手指
     3. minimumPressDuration: 最少按压多久才会响应,默认为0.5秒
     4. allowableMovement: 允许移动多远长按还有响应
     */
   // longPress.numberOfTapsRequired = 2; //默认为0
   // longPress.numberOfTouchesRequired = 2;
  //  longPress.minimumPressDuration = 1;
    longPress.allowableMovement = 100;
    
    
    //添加手势识别对象
    [self.imageView addGestureRecognizer:longPress];
}

//长按的响应
- (void)longPress:(UILongPressGestureRecognizer *)sender {
    NSLog(@"长按了");
}


#pragma mark - 敲击的响应
- (void)tap:(UITapGestureRecognizer *)sender {
    NSLog(@"敲击了");
}


#pragma mark - 敲击
- (void)tap {
    //敲击
    //创建手势识别器对象
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    /**
     UITapGestureRecognizer的两个属性:
     1. numberOfTapsRequired: 需要敲击几次才会响应,默认是1次
     2. numberOfTouchesRequired: 需要几根手指敲击view才会响应,默认是一根手指
     */
    tap.numberOfTapsRequired = 2;
    tap.numberOfTouchesRequired = 2;
    //添加手势
    [self.imageView addGestureRecognizer:tap];
}

@end
